package com.b511.swpu.cementbackend.controller;

import com.b511.swpu.cementbackend.service.MetaDictService;
import com.b511.swpu.cementbackend.service.WellBoreStructureService;
import com.b511.swpu.cementbackend.dto.input.BoreholeConditionInputDto;
import com.b511.swpu.cementbackend.dto.input.MetaDictInputDto;
import com.b511.swpu.cementbackend.entity.WellBoreStructure;
import com.b511.swpu.cementbackend.repository.WellBoreStructureRepository;
import com.b511.swpu.cementbackend.util.R;
import io.swagger.annotations.Api;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(value = "井身结构", tags = {"井身结构"})
@RestController
@RequestMapping("/well_bore_structure")
public class WellBoreStructureController {

    private final WellBoreStructureService wellBoreStructureService;

    private final WellBoreStructureRepository wellBoreStructureRepository;

    private final MetaDictService metaDictService;

    public WellBoreStructureController(WellBoreStructureService wellBoreStructureService,
                                       WellBoreStructureRepository wellBoreStructureRepository,
                                       MetaDictService metaDictService) {
        this.wellBoreStructureService = wellBoreStructureService;
        this.wellBoreStructureRepository = wellBoreStructureRepository;
        this.metaDictService = metaDictService;
    }

    @GetMapping("/{num}/{size}")
    public R getWellBoreStructureByCondition(@PathVariable int num,
                                             @PathVariable int size,
                                             @ModelAttribute BoreholeConditionInputDto boreholeConditionInputDto) {
        return wellBoreStructureService.getWellBoreStructureByCondition(boreholeConditionInputDto, PageRequest.of(num, size));
    }

    @PutMapping("/{id}")
    public R updateWellBoreStructure(@PathVariable Long id, @RequestBody WellBoreStructure updateWellBoreStructure) {
        return wellBoreStructureService.updateWellBoreStructure(id, updateWellBoreStructure);
    }

    @DeleteMapping("/{id}")
    @Transactional
    public R deleteWellBoreStructureByCondition(@PathVariable Long id) {
        wellBoreStructureRepository.deleteById(id);
        return R.ok();
    }

    @GetMapping("/dict/child")
    public R getDictByName(@ModelAttribute @Validated MetaDictInputDto metaDictInputDto) {
        return metaDictService.getDictByName(metaDictInputDto.getLabel());
    }
}
